package zw_301_400.zw_345_反转字符串中的元音字母;

class Solution {
    public String reverseVowels(String s) {
        int len = s.length();
        char[] arr = s.toCharArray();
        int left = 0, right = s.length() - 1;
        while (left < right) {
            while (left < len && !isVowel(arr[left])) {
                ++left;
            }
            while (right > 0 && !isVowel(arr[right])) {
                --right;
            }
            if (left < right) {
                swap(arr, left, right);
                ++left;
                --right;
            }
        }
        return new String(arr);
    }

    public boolean isVowel(char ch) {
        return "aeiouAEIOU".indexOf(ch) >= 0;
    }

    public void swap(char[] arr, int i, int j) {
        char temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
